import { Manager } from "../../manager/Manager";
import ItemScrollCom from "../../component/itemscroll/ItemScrollCom";
import ItemCell from "../../component/itemscroll/ItemCell";
import { EquipTipsData, EquipTipsType } from "../../common/Common_Define";
import { Notifier } from "../../framework/Notifier";
import { ListenID } from "../../ListenID";
import { MVC } from "../../framework/MVC";
import GuajiManager from "../../manager/GuajiManager";
import { Util } from "../../utils/Util";
import { GameVoManager } from "../../manager/GameVoManager";
import { AudioType } from "../../manager/AudioManager";
import AdManager from "../../manager/AdManadgr";
import { AlertManager } from "../../alert/AlertManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GuajiRewardView extends MVC.BaseView {
    @property(ItemScrollCom)
    itemScroll: ItemScrollCom = null;

    changeListener() { }


    onOpen(param) {
        Notifier.send(ListenID.Fight_Pause, true);
        this.setInfo(param);
    }

    setInfo(param) {
        this.initScroll();
    }

    initScroll() {
        console.log(GuajiManager.inst.rewardList)
        /** 初始化物品掉落滑动面板 */
        let itemList = [];
        if (GuajiManager.inst.goldInCome) {
            itemList.push({ itemID: 1, goodsNum: GuajiManager.inst.goldInCome });
        }
        if (GuajiManager.inst.diamondInCome) {
            itemList.push({ itemID: 2, goodsNum: GuajiManager.inst.diamondInCome });
        }
        for (let i = 0; i < GuajiManager.inst.rewardList.length; ++i) {
            itemList.push({ itemID: GuajiManager.inst.rewardList[i] });
        }
        this.itemScroll.setItemList(itemList);
        this.itemScroll.setClickItemHandler((itemCell: ItemCell) => {
            let itemNum = itemCell.getItemNum();
            if (!itemNum) {
                let equipTipsData: EquipTipsData = {
                    type: EquipTipsType.Normal,
                    itemLeftID: itemCell.getItemID(),
                    comparePosNode: itemCell.node,
                    pos: cc.v2(0, 200)
                }
                Notifier.send(ListenID.Equip_OpenTipsView, equipTipsData);
            }
        });
    }

    public runGoldAnim(mult: number = 1) {
        Util.showGoldEffect(this.node, 0, cc.v2(0, 0), cc.v2(-317, 594), 0, 0.2);
        this.scheduleOnce(() => {
            // GameVoManager.getInstance.saveData();
            this.onClose();
        }, 1);
    }


    private onClickNormalBtn() {
        Manager.audio.playAudio(501, AudioType.UI);
        this.runGoldAnim();
    }

    private onClickMultBtn() {
        Manager.audio.playAudio(501, AudioType.UI);
        AdManager.inst.playReward(() => {
            let coin = GuajiManager.inst.goldInCome * 0.5;
            AlertManager.showNormalTips(`恭喜您看视频增加了${coin}金币`);
            GameVoManager.getInstance.setGold(coin);
            this.runGoldAnim(); this.runGoldAnim();
        })

    }

    public onClose(): void {
        if (this._isClosed) return;
        super.onClose();
        Manager.audio.playAudio(501);
        Notifier.send(ListenID.Fight_Pause, false);
    }

    update(dt) {
        // Manager.loader.Update(dt);
    }

}
